SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE Proc [dbo].[SP_TASALD_EMPR_I01]   
/*---------------------------------------------------------------------*/  
/*----- Empresa          : OFISIS S.A.                            -----*/  
/*----- Cliente          : OFISIS S.A.                            -----*/  
/*----- Sistema          : Contabilidad                           -----*/  
/*----- Modulo           : Empresa                                -----*/  
/*----- Programa         : Mayorizacion                           -----*/  
/*----- Nombre           : SP_TASALD_EMPR_I01                   -----*/  
/*----- Script           : TASALD_EMPR                            -----*/  
/*----- Desarrollado     : Israel Estrada Polanco                 -----*/  
/*----- Fecha            : 23/06/1997                             -----*/  
/*----- Base de Datos    : MS SQL Server                          -----*/  
/*----- Version BD       : 6.5                                    -----*/  
/*----- Invoca a SP      :                                        -----*/  
/*---------------------------------------------------------------------*/  
/*---------------------------------------------------------------------*/  
/*----- Modificado 1 por : percy garcia                           -----*/  
/*----- Fecha Modifica   : 05/06/2004                             -----*/  
/*----- Detalle Modifica : usuario creacion y modif               -----*/  
/*---------------------------------------------------------------------*/  
/*---------------------------------------------------------------------*/  
/*----- Comentarios      :                                        -----*/  
/*---------------------------------------------------------------------*/  
/*---------------------------------------------------------------------*/  
/*----- Drop Proc SP_TASALD_EMPR_I01                              -----*/  
/*----- Grant Execute On SP_TASALD_EMPR_I01 To PUBLIC             -----*/  
/*----- SP_TASALD_EMPR_I01                                        -----*/  
/*---------------------------------------------------------------------*/  
@ISCO_EMPR TD_VC_002,  
@ISCO_USUA          TD_VC_008,
@IDFE_USUA          TD_DT_001,
@ISCO_UNID_CNTB TD_VC_003,  
@INNU_CNTB_EMPR TD_IN_001_CONT,  
@INNU_ANO1 TD_IN_004_ANNO,  
@INNU_MES1 TD_IN_002_MESE,  
@INNU_ANO2 TD_IN_004_ANNO,  
@INNU_MES2 TD_IN_002_MESE  
  
As  
Begin Transaction SP_TASALD_EMPR_I01  
Declare  
@VSCO_CNTA_EMPR TD_VC_020_CNTA,  
@VNNU_ANOX TD_IN_004_ANNO,  
@VNNU_MESX TD_IN_002_MESE,  
@VNNU_ANOY TD_IN_004_ANNO,  
@VNNU_MESY TD_IN_002_MESE,  
@VSPR_DATO TD_VC_020,  
@VNNV_INFO_CNTA TD_IN_001,  
@VSNV_QUIE_CNTA TD_VC_020,  
@VNNU_MES1 TD_IN_002_MESE,  
@VNNU_MES2 TD_IN_002_MESE  
                                                                                                                                                                                     
Select @VNNU_ANOX = @INNU_ANO1  
Select @VNNU_MESX = @INNU_MES1  
Select @VNNU_ANOY = @INNU_ANO2  
Select @VNNU_MESY = @INNU_MES2  
                                                                                                                                                                                                     
/* Elimina registros de la tabla de saldos para la empresa, */  
Delete From TASALD_EMPR  
 Where CO_EMPR = @ISCO_EMPR  And  
       NU_CNTB_EMPR = @INNU_CNTB_EMPR And  
       NU_ANNO  = @INNU_ANO1 And   
       NU_MESE Between @INNU_MES1 And @INNU_MES2      
  
-- Saca Datos de Informacion  
  Select @VNNV_INFO_CNTA = NV_INFO_CNTA,  
         @VSNV_QUIE_CNTA = NV_QUIE_CNTA   
  From   TMPARA_CONT  
  Where  CO_EMPR = @ISCO_EMPR  
  
 Select @VNNU_MES1 = @INNU_MES1  
 Select @VNNU_MES2 = @INNU_MES2  
   
 While @INNU_MES1 <= @INNU_MES2  
  Begin  
     Select @VSPR_DATO  = replicate('0',@INNU_MES1) + '1' + replicate('0',20-(@INNU_MES1+1))  
        
     Insert Into TASALD_EMPR (CO_EMPR, CO_UNID_CNTB, NU_CNTB_EMPR, NU_ANNO, NU_MESE,   
         CO_CNTA_EMPR, TI_AUXI_EMPR, CO_AUXI_EMPR, CO_ORDE_SERV, IM_CARG_CNTB, IM_ABON_CNTB,  
         IM_CARG_ORIG, IM_ABON_ORIG , PR_DATO , CO_USUA_MODI, FE_USUA_MODI, CO_USUA_CREA, FE_USUA_CREA)  
     Select @ISCO_EMPR, T2.CO_UNID_CNTB, @INNU_CNTB_EMPR, @INNU_ANO1, @INNU_MES1, T1.CO_CNTA_EMPR,   
        NULL,NULL, NULL, 0, 0, 0, 0, @VSPR_DATO, @ISCO_USUA, @IDFE_USUA, @ISCO_USUA, @IDFE_USUA  
      From TMPLAN_EMPR T1, TMUNID_CNTB T2  
     Where T1.CO_EMPR = @ISCO_EMPR  
     And T1.NU_ANNO  = 9999  
     And DataLength(T1.CO_CNTA_EMPR) = @VNNV_INFO_CNTA  
     And T1.CO_EMPR = T2.CO_EMPR  
    
    If @@ERROR <> 0     /*si falla el Insert*/  
     Begin   
         Rollback Transaction  
         Return  
     End                                                                                                                                                                                                         
                                                 
    Select @INNU_MES1 = @INNU_MES1 + 1  
  
  End  
                                                                                                                                                                       
 Exec SP_TASALD_EMPR_I02 @ISCO_EMPR, @ISCO_USUA, @IDFE_USUA, @ISCO_UNID_CNTB, @INNU_CNTB_EMPR, @VNNU_ANOX , @VNNU_MESX,  
                        @VNNU_ANOY, @VNNU_MESY                                          
 If @@ERROR <> 0  
   Begin   
     Rollback Transaction  
     Return  
   End  
                                                                                                                                                                                                                                             
 Exec SP_TASALD_EMPR_I03 @ISCO_EMPR, @ISCO_USUA, @IDFE_USUA, @ISCO_UNID_CNTB, @INNU_CNTB_EMPR, @VNNU_ANOX , @VNNU_MESX,  
                        @VNNU_ANOY, @VNNU_MESY  
                                         
 If @@ERROR <> 0  
   Begin   
     Rollback Transaction  
     Return  
   End  
                  
 Exec SP_TASALD_FLEF_I01 @ISCO_EMPR,@ISCO_USUA, @IDFE_USUA, @INNU_CNTB_EMPR,@VNNU_ANOX,@VNNU_MESX,@VNNU_ANOY,@VNNU_MESY  
                                                                                                      
    If @@ERROR <> 0  
     Begin   
       Rollback Transaction  
       Return  
     End  
  
  Update TCFECH_PROC  
   Set ST_MAYO_CNTB = 'S',  
       FE_MAYO_CNTB = @IDFE_USUA,
           CO_USUA_MODI = @ISCO_USUA,
           FE_USUA_MODI = @IDFE_USUA  
   Where CO_EMPR = @ISCO_EMPR and  
         NU_CNTB_EMPR = @INNU_CNTB_EMPR And   
         NU_ANNO = @INNU_ANO1 and  
         NU_MESE Between @VNNU_MES1 And @VNNU_MES2  
  
    If @@ERROR <> 0  
      Begin   
       Rollback Transaction  
      Return  
      End  
    Commit Transaction  
  Return  
/*------------------------------- Fin --------------------------------------------*/  

GO
